// pages/qrcode/qrcode.js
import atob from '../../utils/base64'
var util = require('../../utils/util')
Page({

    /**
     * 页面的初始数据
     */
    data: {
        token: "",
        bottleId: "",
        userid: "",
        time:"",
        location:"",
        submit:[]
    },
    //获取试剂瓶id和时间以及地点
    handleBottleId:function (e) {
        var gettime = util.formatTime(new Date())
        this.setData({
            bottleId: e.detail.value,
            time: gettime
        })
        console.log(this.data.time)
        console.log(this.data.bottleId)
    },

    handleLocation:function (e) {
        this.setData({
            location: e.detail.value
        })
        console.log(this.data.location)
    },

    handleSubmit:function () {
        var that = this;
        wx.getStorage({
            key: "usertoken",
            success:(result)=> {
                console.log(result)
                that.setData({
                    token:result.data
                })
            },
            fail:(err)=>{
                console.log('get_token_error')
            }
        });
        
        wx.getStorage({
            key: "userId",
            success:(result)=> {
                console.log(result)
                that.setData({
                    userid:result.data
                })
            },
            fail:(err)=>{
                console.log('get_userid_error')
            }
        })
        console.log(that.data.time)
        console.log(that.data.location)
        console.log(that.data.userid)
        console.log(that.data.bottleId)
        console.log(that.data.token)
        
        var that1 = that;
        wx.request({
          url: 'http://192.168.43.214:9527/natms/reagent/Info',
          method:'POST',
          header :{
            'token':that1.data.token
         },
          data: {
            samplingTime: that1.data.time,
            samplingPlace: that1.data.location,
            reagentBottleId: that1.data.bottleId,
            volunteerId: that1.data.userid
          },
          success:(res)=>{
            console.log(res)
          },
          fail:(err)=>{
            console.log(err)
            if(err.msg === "服务故障"){
                wx.showToast({
                    title: '试剂瓶id重复提交',
                    icon:'error'
                  })
            }

          }
        })
    },

    //在qrcode页面，
    //从缓存中取barcode出来，每次扫描把barcode和qrcode(atob)放进submit[{}]中
    //最后点击提交时将submit提交并清空
    // handleBar:function() {
    //     wx.getStorage({
    //         key: "usertoken",
    //         success:(result)=> {
    //             this.setData({
    //                 token:result.data.token
    //             })
    //         },
    //         fail:(err)=>{
    //             console.log('get_token_error')
    //         }
    //     })
    //     console.log(this.data.token+'_test')
    //     console.log('BarCode')
    // },

    handleQR:function() {
        if(this.data.bottleId !== "" && this.data.location !== "") {
            wx.scanCode({
                onlyFromCamera: false,
                success: (res) =>{
                    this.setData({
                        userid: res.result.substr(0,3),
                    }),
                    this.setData({
                      submit:[
                          {
                              reagentBottleId: this.data.bottleId,
                              userId: this.data.userid
                          },
                          ...this.data.submit
                        ]
                    })
                    console.log(this.data.userid)
                    console.log(this.data.submit)
                    wx.showToast({
                      title: '上传成功',
                    })
                }
              })
            //   console.log('QRCode')
        } else {
            wx.showToast({
                icon: 'error',
                title: '试剂瓶信息不全',
            })
        }

        
    },

    handleUpload:function() {
        console.log(this.data.token)
        console.log(this.data.submit)
        if (this.data.submit.length !== 0) {
            wx.request({
                url: 'http://192.168.43.214:9527/natms/reagent/UserInfo',
                method: 'POST',
                header: {
                    'token':this.data.token
                },
                data: this.data.submit,
                success: (res) => {
                    console.log(res)
                    wx.showToast({
                      title: '提交成功',
                    })
                    this.setData({
                        bottleId: "",
                        userid: "",
                        submit: []
                    })
                }
              })
            //   console.log('upload')
        } else {
            wx.showToast({
                icon: 'error',
                title: '录入数量为0',
            })
        }
    },

    

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})